//
//  XYMeetingViewController.m
//  XYRTCEngineSimpleDemo
//
//  Created by 高超 on 2021/11/10.
//

#import "XYMeetingViewController.h"
#import "XYVideoView.h"

@interface XYMeetingViewController ()

@property (nonatomic, strong) NSMutableArray <NemoVideoCanvas *>*currentVideos;
@property (nonatomic, strong) NemoLayout *localLayout;

@end

@implementation XYMeetingViewController

- (void)dealloc {
    NSLog(@"%s",__FUNCTION__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = XYColor(@"#22222B", 1);
    [XYSDKManager share].meetingVC = self;
    _currentVideos = [[NSMutableArray alloc] init];
    
    [[NemoSDK sharedInstance] enableVideo:YES];
    [[NemoSDK sharedInstance] startAudio];
    
    [self videoChange:nil];
}

- (void)videoChange:(NSArray *_Nullable)videos {
    NSMutableArray *allVideos = [NSMutableArray arrayWithArray:videos];
    [allVideos insertObject:self.localLayout atIndex:0];
    NSMutableArray *removeVideos = [NSMutableArray array];
    // 过滤video，更新存在video，保存不存在video
    for (NemoVideoCanvas *video in self.currentVideos) {
        BOOL found = NO;
        for (NemoLayout *layout in allVideos) {
            if ([self equalLayout:layout with:video.layout] || video.layout == self.localLayout) {
                XYVideoView *cusView = (XYVideoView *)video.view;
                found = YES;
                video.layout = layout;
                cusView.layout = layout; //更新本地展示的view
                [[NemoSDK sharedInstance] updateVideo:video]; //更新视频
                [allVideos removeObject:layout];
                break;
            }
        }
        if (!found) {
            [removeVideos addObject:video];
        }
    }
    // 移除不存在video，并且移除View
    for (NemoVideoCanvas *video in removeVideos) {
        [self.currentVideos removeObject:video];
        [video clear];
        [video.view removeFromSuperview];
    }
    //创建新的video
    for (NSInteger i = 0; i < allVideos.count; i++) {
        NemoVideoCanvas *video = [[NemoVideoCanvas alloc] init];
        XYVideoView *view = [[XYVideoView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        video.view = view;
        video.layout = allVideos[i];
        if (allVideos[i] == self.localLayout) {
            video.isLocal = YES;
        }
        view.layout = allVideos[i];
        [[NemoSDK sharedInstance] setupVideo:video]; //创建视频
        [self.view addSubview:view];
        [self.currentVideos addObject:video];
    }
    [self updateFrame];
}

- (void)updateFrame {
    CGFloat width = kScreen.width/2;
    CGFloat height = width * 9/16;
    for (NSInteger i = 0; i < self.currentVideos.count; i++) {
        NemoVideoCanvas *canvas = self.currentVideos[i];
        XYVideoView *view = (XYVideoView *)canvas.view;
        view.frame = CGRectMake(width*(i%2), 100+height*(i/2), width, height);
    }
}

- (BOOL)equalLayout:(NemoLayout *)first with:(NemoLayout *)other{
    if (first.streamID.length && [first.streamID isEqualToString:other.streamID]) {
        return YES;
    } else if ([first.participantId isEqualToString:other.participantId]){
        return YES;
    } else {
        return NO;
    }
}

- (NemoLayout *)localLayout {
    if (!_localLayout) {
        _localLayout = [[NemoLayout alloc] init];
        _localLayout.layoutState = NemoStreamState_Received;
//        _localLayout.remoteName = [XYSDKManager share].user.displayName;
    }
    return _localLayout;
}

@end
